package org.springblade.mach.utils;

import io.minio.errors.*;
import lombok.AllArgsConstructor;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springblade.mach.entity.ReadExcelEntity;
import org.springblade.mach.service.IDrawingNumberApplicationDetailService;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Component
@AllArgsConstructor
public class MachUtils {
	private ExcelUtils excelUtils;
	// 图号申请详情
	private IDrawingNumberApplicationDetailService drawingNumberApplicationDetailService;

	public ReadExcelEntity getBomOwnAttribute(ReadExcelEntity readExcelEntity, Map<String, Object> map) throws InvalidBucketNameException, InsufficientDataException, IOException, NoSuchAlgorithmException, InvalidPortException, InvalidKeyException, InvalidEndpointException, InternalException, InvalidArgumentException, XmlPullParserException, ErrorResponseException, NoResponseException {
		final List<ReadExcelEntity> excelList = excelUtils.getExcelEntityList("000000-machtech", (String) map.get(readExcelEntity.getDrawingNumber().trim()));
		for (ReadExcelEntity excelEntity : excelList) {
			if (excelEntity.getDrawingNumber().trim().equals(readExcelEntity.getDrawingNumber().trim())) {
				readExcelEntity.setMaterial(excelEntity.getMaterial());
				readExcelEntity.setSingleWeight(excelEntity.getSingleWeight());
				readExcelEntity.setDrawingProperty(excelEntity.getDrawingProperty());
				readExcelEntity.setDrawingRemark(excelEntity.getDrawingRemark());
				readExcelEntity.setRemark(excelEntity.getRemark());
				readExcelEntity.setCharacOne(excelEntity.getCharacOne());
				readExcelEntity.setCharacTwo(excelEntity.getCharacTwo());
				readExcelEntity.setCharacThree(excelEntity.getCharacThree());
				break;
			}
		}
		return readExcelEntity;
	}


	/**
	 * 校验excel中代号是否申请过,返回错误信息
	 */
	public List<String> verifyExcel(MultipartFile file) {
		List<String> drawingNumberList = readExcelCode(file);
		// step1:先检测excel中的代号信息在数据中是否存在
		List<String> errorMsgList = new ArrayList<>();
		for (int i = 0; i < drawingNumberList.size(); i++) {
			// 图号是否有申请记录
			Integer count = drawingNumberApplicationDetailService.getCountByDrawingNumber(drawingNumberList.get(i));
			if (count == 0) {
				errorMsgList.add("excel文件中代号" + drawingNumberList.get(i) + "未申请图号");
				continue;
			}
		}
		return errorMsgList;
	}


	/**
	 * @Author zhp
	 * @Description //TODO
	 * @Date 16:53 2022/1/20
	 * @return
	 * 读取excel中代码信息
	*/
	public List<String> readExcelCode(MultipartFile file) {
		Workbook wb = ExcelUtils.getWb(file);
		return getDrawingNumberByExcel(wb);
	}

	/**
	 * @Author zhp
	 * @Description //TODO
	 * @Date 17:44 2022/1/20
	 * @param wb
	 * @return java.util.List<java.lang.String>
	 * 获取Excel中的所有的图号信息
	*/
	public List<String> getDrawingNumberByExcel(Workbook wb) {
		Sheet sheetAt = wb.getSheetAt(0);
		List<String> drawingNumberList = new ArrayList<>();
		for (int i = 0; i <= sheetAt.getLastRowNum(); i++) {
			Row row = sheetAt.getRow(i);//获取每一行的数据
			String drawingNumberFiles = String.valueOf(row.getCell(1).getStringCellValue());
			boolean contains = drawingNumberFiles.contains("MH");
			if (contains) {
				drawingNumberList.add(drawingNumberFiles);
			}
		}
		return drawingNumberList;
	}
}
